//
//  SideMenuContentViewController.m
//  JiuLe
//
//  Created by xcq on 15/11/1.
//  Copyright © 2015年 Fine. All rights reserved.
//

#import <GTSDK/GeTuiSdk.h>
#import "JLVersionUpdate.h"
#import "BaseNavController.h"
#import "LoginViewController.h"
#import "LeftSideMenuContent.h"
#import "UIColor+RGBConverHex.h"
#import "HomePageViewController.h"
#import "JLSideMenuTableViewCell.h"
#import "SideMenuContentViewController.h"
#define kSideMenuNum 8
static NSString *identifier = @"sideMenuContentCell";
@interface SideMenuContentViewController() <UITableViewDataSource,UITableViewDelegate>
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *topLaytou;
@property (weak, nonatomic) IBOutlet UIImageView *companyLogo;
@property (strong, nonatomic) NSArray *titleArr;
@end

@implementation SideMenuContentViewController


- (void)viewDidLoad {
    [super viewDidLoad];
//    NSUserDefaults seto
    if (![[NSUserDefaults standardUserDefaults] boolForKey:JiuLeOfflineMode]) {
        _titleArr = @[@"首页",
//                      @"连接",
//                      @"健康阀值设置",
//                      @"提醒设置",
//                      @"来电设置",
                      @"关于我们",
                      @"退出"];
    } else {
        _titleArr = @[@"连接",
                      @"健康阀值设置",
                      @"提醒设置",
                      @"退出"];
    }
   
    
    _tableView.rowHeight = 40;
    _tableView.delegate = self;
    if (iPhone4s) {
        _tableView.scrollEnabled = YES;
    } else {
        _tableView.scrollEnabled = NO;
    }
    _tableView.showsHorizontalScrollIndicator = NO;
    _tableView.showsVerticalScrollIndicator = NO;
    _tableView.dataSource = self;
    [self SeparatorForTableView:_tableView];
    if (iPhone6) {
        _topLaytou.constant = 14;
    } else if(iPhone6P){
        _topLaytou.constant = 18;
        
    }
}

- (void)viewWillAppear:(BOOL)animated {
    self.navigationController.navigationBarHidden = YES;
}

- (void)viewWillDisappear:(BOOL)animated {
    self.navigationController.navigationBarHidden = NO;
}

-(void)SeparatorForTableView:(UITableView *)tableView {
    if ([tableView respondsToSelector:@selector(setSeparatorInset:)]) {
        [tableView setSeparatorInset:UIEdgeInsetsMake(0,0,0,0)];
    }
    
    if ([tableView respondsToSelector:@selector(setLayoutMargins:)]) {
        [tableView setLayoutMargins:UIEdgeInsetsMake(0,0,0,0)];
    }
    if (!tableView.tableFooterView) {
        [tableView setTableFooterView:[[UIView alloc] initWithFrame:CGRectZero]];
    }
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _titleArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView
         cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    JLSideMenuTableViewCell *cell = [[[NSBundle mainBundle] loadNibNamed:@"JLSideMenuTableViewCell" owner:self options:nil] lastObject];
    
    cell.icon.image = [UIImage imageNamed:[NSString stringWithFormat:@"slide_%zd",indexPath.row]];
    cell.name.textColor = [UIColor darkGrayColor];
    cell.name.text = _titleArr[indexPath.row];
    cell.backgroundColor = [UIColor colorWithHex:0xE5E9EB];
    cell.contentView.backgroundColor = [UIColor colorWithHex:0xE5E9EB];
    return cell;
}

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
    if ([cell respondsToSelector:@selector(setSeparatorInset:)]) {
        [cell setSeparatorInset:UIEdgeInsetsMake(0,0,0,0)];
    }
    
    if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {
        [cell setLayoutMargins:UIEdgeInsetsMake(0,0,0,0)];
    }
    
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    switch (indexPath.row) {
        case 0:
        {
            if ([[NSUserDefaults standardUserDefaults] boolForKey:JiuLeOfflineMode]) {
                ConnectionDeviceViewController *activate = [[ConnectionDeviceViewController alloc] initWithNibName:@"ConnectionDeviceViewController" bundle:nil];
                BaseNavController *active = [[BaseNavController alloc] initWithRootViewController:activate];
                [self.sideMenuViewController setContentViewController:active animated:YES];
                [self.sideMenuViewController hideMenuViewController];
             } else {
                if ([self.sideMenuViewController.contentViewController isKindOfClass:[CYLTabBarController class]]) {
                    [self.sideMenuViewController  hideMenuViewController];
                } else {
                    JlUserManager *manager = [JlUserManager shareInstance];
                    [self.sideMenuViewController setContentViewController:manager.baseTab animated:YES];
                    [self.sideMenuViewController hideMenuViewController];
                }
            }
        }
            break;
//        case 1: {
//            if ([[NSUserDefaults standardUserDefaults] boolForKey:JiuLeOfflineMode]) {
//                HealthThresholdsTableViewController *health = [[HealthThresholdsTableViewController alloc] initWithNibName:@"HealthThresholdsTableViewController" bundle:nil];
//                BaseNavController *active = [[BaseNavController alloc] initWithRootViewController:health];
//                [self.sideMenuViewController setContentViewController:active animated:YES];
//                [self.sideMenuViewController hideMenuViewController];
//            } else {
//                ConnectionDeviceViewController *activate = [[ConnectionDeviceViewController alloc] initWithNibName:@"ConnectionDeviceViewController" bundle:nil];
//                BaseNavController *active = [[BaseNavController alloc] initWithRootViewController:activate];
//                [self.sideMenuViewController setContentViewController:active animated:YES];
//                [self.sideMenuViewController hideMenuViewController];
//            }
//               //            }
//        }
//            break;
//        case 2: {
//            if ([[NSUserDefaults standardUserDefaults] boolForKey:JiuLeOfflineMode]) {
//                SettingAlarmViewController *health = [[SettingAlarmViewController alloc] initWithNibName:@"SettingAlarmViewController" bundle:nil];
//                BaseNavController *active = [[BaseNavController alloc] initWithRootViewController:health];
//                [self.sideMenuViewController setContentViewController:active animated:YES];
//                [self.sideMenuViewController hideMenuViewController];
//            } else {
//                HealthThresholdsTableViewController *health = [[HealthThresholdsTableViewController alloc] initWithNibName:@"HealthThresholdsTableViewController" bundle:nil];
//                BaseNavController *active = [[BaseNavController alloc] initWithRootViewController:health];
//                [self.sideMenuViewController setContentViewController:active animated:YES];
//                [self.sideMenuViewController hideMenuViewController];
//            }
//                   }
//            break;
//        case 3: {
//            if ([[NSUserDefaults standardUserDefaults] boolForKey:JiuLeOfflineMode]) {
//                [[[UIAlertView alloc] initWithTitle:@"提示"
//                                            message:@"确定要退出吗？"
//                                           delegate:self
//                                  cancelButtonTitle:@"取消"
//                                  otherButtonTitles:@"确定", nil]
//                 show];
//            } else {
//                SettingAlarmViewController *health = [[SettingAlarmViewController alloc] initWithNibName:@"SettingAlarmViewController" bundle:nil];
//                BaseNavController *active = [[BaseNavController alloc] initWithRootViewController:health];
//                [self.sideMenuViewController setContentViewController:active animated:YES];
//                [self.sideMenuViewController hideMenuViewController];
//            }
//        }
//            break;
//        case 4: {
//
//            CallMeTypeViewController *health = [[CallMeTypeViewController alloc] initWithNibName:@"CallMeTypeViewController" bundle:nil];
//            BaseNavController *active = [[BaseNavController alloc] initWithRootViewController:health];
//            [self.sideMenuViewController setContentViewController:active animated:YES];
//            [self.sideMenuViewController hideMenuViewController];
//        }
//            break;
////        case 5: {
////            //ServiceZoneViewController
////            PwdChangeViewController *health = [[PwdChangeViewController alloc] initWithNibName:@"PwdChangeViewController" bundle:nil];
////            BaseNavController *active = [[BaseNavController alloc] initWithRootViewController:health];
////            [self.sideMenuViewController setContentViewController:active animated:YES];
////            [self.sideMenuViewController hideMenuViewController];
////        }
//            break;
        case 1:{
            JLVersionUpdate *version = [[JLVersionUpdate alloc] initWithNibName:@"JLVersionUpdate" bundle:nil];
             BaseNavController *active = [[BaseNavController alloc] initWithRootViewController:version];
            [self.sideMenuViewController setContentViewController:active animated:YES];
            [self.sideMenuViewController hideMenuViewController];
        }break;
        case 2: {
            [[[UIAlertView alloc] initWithTitle:@"提示"
                                        message:@"确定要退出吗？"
                                       delegate:self
                              cancelButtonTitle:@"取消"
                              otherButtonTitles:@"确定", nil]
             show];
        }
        default:
            break;
    }
}


- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
    if (buttonIndex == 1) {
        [GeTuiSdk unbindAlias:[JlUserManager shareInstance].userInfo.userid];
        [[NSUserDefaults standardUserDefaults]setBool:NO forKey:JiuLeOfflineMode];
        [[NSUserDefaults standardUserDefaults] removeObjectForKey:UserDataPassWord];
        [[NSUserDefaults standardUserDefaults] setBool:NO forKey:JiuLeIsConnection];
        [[NSUserDefaults standardUserDefaults] setObject:@"" forKey:JiuLeUserName];
        [[NSUserDefaults standardUserDefaults] setObject:@(NO) forKey:@"push"];
        LoginViewController *login = getStoryOfControllerInstance(@"LoginViewController");
        self.view.window.rootViewController = login;
    }
}
@end
